/*文件注释
 * ===============================================================================
 * 说明：
 * 作者：zengyy
 * 版本：v1.0
 * 创建日期：2013-05-04 16:45:21
 * 完成日期： 
 * 修改纪录：  
 * ===============================================================================
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;

namespace SRF.Admin
{
    /// <summary>
    /// 部门 Service
    /// </summary>
    public class DepartmentService
    {
        readonly Data.DBHelperBase dbhelper = Data.DBHelperFactory.GetDBHelper();

        /// <summary>
        /// 获取部门
        /// </summary>
        /// <param name="id">部门id</param>
        /// <returns>返回部门</returns>
        public Models.Department Get(string id)
        {
            return dbhelper.GetQuery("admin.department.get").SetParameter(new { Id = id }).UniqueResult<Models.Department>();
        }
        /// <summary>
        /// 获取部门列表
        /// </summary>
        /// <returns>返回部门列表</returns>
        public IList<Models.Department> List()
        {
            return dbhelper.GetQuery("admin.department.list").List<Models.Department>();
        }
        /// <summary>
        /// 保存部门
        /// </summary>
        /// <param name="post">部门</param>
        /// <returns>返回部门Id</returns>
        public string Save(Models.Department department)
        {
            if (string.IsNullOrEmpty(department.Id))
            {
                department.Id = Guid.NewGuid().ToString();
                dbhelper.GetQuery("admin.department.insert").SetParameter(department).Execute();
            }
            else
            {
                dbhelper.GetQuery("admin.department.update").SetParameter(department).Execute();
            }

            return department.Id;
        }
        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="id">部门Id</param>
        public void Delete(string id)
        {
            dbhelper.GetQuery("admin.department.delete").SetParameter("Id", id).Execute();
        }
        /// <summary>
        /// 是否有子部门或用户属于该部门
        /// </summary>
        /// <param name="id">部门Id</param>
        public bool DeleteCheck(string id)
        {
            return dbhelper.GetQuery("admin.department.deletecheck").SetParameter("Id", id).Exists();
        }
    }
}